package com.maomao.maomaochong2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by maomao on 2017/4/28.
 */

public class MyListAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<AllItem> allItem;
    private int mao = -1;

    public MyListAdapter(String[] titles, Context context){
        super();
        allItem = new ArrayList<AllItem>();
        inflater = LayoutInflater.from(context);
        for (int i = 0; i < titles.length; i++) {
            AllItem litem = new AllItem(titles[i]);
            allItem.add(litem);
        }
    }

    public int getCount(){
        if(null != allItem){
            return allItem.size();
        } else{
            return 0;
        }
    }

    public Object getItem(int arg0){
        return allItem.get(arg0);
    }

    public long getItemId(int arg0){
        return arg0;
    }

    public View getView(int arg0, View arg1, ViewGroup arg2){
        MyViewHolder holder;
        if(arg1 == null){
            arg1 = inflater.inflate(R.layout.list_item,null);
            holder = new MyViewHolder();
            holder.holderTitle = (TextView) arg1.findViewById(R.id.listText);
            arg1.setTag(holder);
        } else {
            holder = (MyViewHolder) arg1.getTag();
        }
        holder.holderTitle.setText(allItem.get(arg0).getName());

        if(mao == arg0) {
            arg1.setBackgroundColor(0xffffffff);
            holder.holderTitle.setTextColor(0xffff7257);
        } else{
            arg1.setBackgroundColor(0xffdbdbdb);
            holder.holderTitle.setTextColor(0xff000000);
        }
        return arg1;
    }

    public void setSeclection(int position){
        mao = position;
    }

}
class MyViewHolder{
    public TextView holderTitle;
}
